package Server;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class ServerStartupGUI {

	private static final int WIDTH = 300;
	private static final int HEIGHT = 200;

	private JFrame frame;
	private JPanel northPanel;
	private JPanel southPanel;

	private JButton startButton;
	private JButton defaultButton;
	private JButton singlePlayerButton;


	private JLabel numPlayerLabel;
	private JLabel numColumnsLabel;
	private JLabel numLinesLabel;
	private JLabel numBarriersLabel;
	private JLabel numItemsLabel;

	private JTextField numPlayerField;
	private JTextField numColumnsField;
	private JTextField numLinesField;
	private JTextField numBarriersField;
	private JTextField numItemsField;

	private Server server;

	public ServerStartupGUI(Server server) {
		this.server = server;
		frame = new JFrame("Server Startup!!! Snake Multiplayer");
		northPanel = new JPanel();
		southPanel = new JPanel();
		startButton = new JButton("Start");
		defaultButton = new JButton("Default");
		singlePlayerButton = new JButton("SinglePlayer");
		init();
	}

	private void init() {
		frame.setSize(WIDTH, HEIGHT);
		frame.setResizable(false);
		frame.setLayout(new BorderLayout());
		frame.add(NorthPanel(), BorderLayout.CENTER);
		frame.add(SouthPanel(), BorderLayout.SOUTH);
		frame.setLocationRelativeTo(null);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
	}

	private JPanel NorthPanel() {
		numPlayerLabel = new JLabel("How Many Players Will be Joining?");
		numPlayerField = new JTextField(5);
		numColumnsLabel = new JLabel("How Many Columns Will be There?");
		numColumnsField = new JTextField(5);
		numLinesLabel = new JLabel("How Many Lines Will be There?");
		numLinesField = new JTextField(5);
		numBarriersLabel = new JLabel("How Many Barriers on The Screen?");
		numBarriersField = new JTextField(5);
		numItemsLabel = new JLabel("How Many Items on the Screen?");
		numItemsField = new JTextField(5);
		northPanel.setLayout(new FlowLayout());
		northPanel.add(numPlayerLabel);
		northPanel.add(numPlayerField);
		northPanel.add(numColumnsLabel);
		northPanel.add(numColumnsField);
		northPanel.add(numLinesLabel);
		northPanel.add(numLinesField);
		northPanel.add(numBarriersLabel);
		northPanel.add(numBarriersField);
		northPanel.add(numItemsLabel);
		northPanel.add(numItemsField);
		return northPanel;
	}

	private JPanel SouthPanel() {
		southPanel.setLayout(new FlowLayout());
		southPanel.add(startButton);
		southPanel.add(defaultButton);
		southPanel.add(singlePlayerButton);
		startButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				if (!numPlayerField.getText().equals("")
						&& !numColumnsField.getText().equals("")
						&& !numLinesField.getText().equals("")
						&& !numBarriersField.getText().equals("")
						&& !numItemsField.getText().equals("")) {
					frame.dispose();
					server.runServer();
				} else {
					JOptionPane.showMessageDialog(null,
							"Fill All the Blank Fields...");
				}
			}

		});
		defaultButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				numPlayerField.setText("2");
				numColumnsField.setText("30");
				numLinesField.setText("30");
				numBarriersField.setText("20");
				numItemsField.setText("20");
				
			}
		});
		singlePlayerButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				numPlayerField.setText("1");
				numColumnsField.setText("30");
				numLinesField.setText("30");
				numBarriersField.setText("20");
				numItemsField.setText("20");
			}
		});
		
		return southPanel;
	}

	public static int getWidth() {
		return WIDTH;
	}

	public static int getHeight() {
		return HEIGHT;
	}

	public JTextField getNumPlayerField() {
		return numPlayerField;
	}

	public JTextField getNumColumnsField() {
		return numColumnsField;
	}

	public JTextField getNumLinesField() {
		return numLinesField;
	}

	public JTextField getNumBarriersField() {
		return numBarriersField;
	}

	public JTextField getNumItemsField() {
		return numItemsField;
	}

}
